Set Up

  • Install/load necessary R packages
  • Set working directory if necessary (or create a file path to use throughout the RMD to call the data from Box)

Read in data from CSVs in the intermediate_data directory

#read in necessary CSV files from prior RMDs
all_streamflow <- read_csv(here("intermediate_data", "all_streamflow.csv"))
air_temp_30_clean <- read_csv(here("intermediate_data", "air_temp_30_clean.csv"))
mc_clean <- read_csv(here("intermediate_data", "mc_clean.csv")) %>% 
  rename(datetime = collected)
precip_15_clean <- read_csv(here("intermediate_data", "precip_15_clean.csv")) 
precip_6h_clean <- read_csv(here("intermediate_data", "precip_6h_clean.csv")) 
precip_12h_clean <- read_csv(here("intermediate_data", "precip_12h_clean.csv")) 
precip_daily_clean <- read_csv(here("intermediate_data", "precip_daily_clean.csv")) 
precip_daily_join <- read_csv(here("intermediate_data", "precip_daily_join.csv")) 

Set constant values (aka the date range of the analysis)

#set constants 
min_year = 2017
max_year = 2021

Create sub-directories if necessary

output_dir <- file.path(here("intermediate_data"))

if (!dir.exists(output_dir)){
dir.create(output_dir)
} else {
    print("intermediate_data directory already exists!")
}
## [1] "intermediate_data directory already exists!"
output_dir <- file.path(here("figures"))

if (!dir.exists(output_dir)){
dir.create(output_dir)
} else {
    print("figures directory already exists!")
}
## [1] "figures directory already exists!"

Daily Cumulative Precipitation + Air Temperature Every 30min

For interactive plots,

  • air temperature (ºC / 100) is plotted as a pink line, and

  • cumulative daily precipitation (in / 10) is plotted as grey bars.

#format precipitation data for plotting 
precip_15_join <- precip_15_clean %>% 
  pivot_wider(
    names_from = "precip_type",
    values_from = "precip_in"
  ) %>% 
  subset(year >= min_year & year <= max_year) %>% 
  rename(precip_10_in = precip_10)

precip_daily_join <- precip_daily_join %>% 
  mutate(dummy = "b",
         dummy = as.factor(dummy))

#format air temperature data for plotting 
airtemp_join <- air_temp_30_clean %>% 
  pivot_wider(
    names_from = "airtemp_type",
    values_from = "airtempC"
  ) %>% 
  subset(year >= min_year & year <= max_year) %>% 
  mutate(freezing = as.factor(freezing),
         dummy = "a")

#format air temp data for plotting 
precip_air <- full_join(x = precip_15_join, 
                        y = airtemp_join, 
                        by = c("datetime", "year")) %>% 
  subset(year >= min_year & year <= max_year)

#join air and precipitation data 
precip_air <- full_join(x = precip_15_join, 
                        y = airtemp_join, 
                        by = c("datetime", "year")) %>% 
  subset(year >= min_year & year <= max_year)

#plot
p_daily <- ggplot() + 
  #airtemp data
  geom_line(data = airtemp_join, 
            aes(x = datetime, 
                y = airtempc_100,
                color = dummy,
                group = 1,
                #create hovering labels for interactive graph 
                text = paste0("DateTime: ", datetime, "\n",
                              "AirTemp (ºC/100): ", airtempc_100)
                ),
            color = "pink",
            alpha = 0.5,
            size = 0.25) + 
  #precipitation data 
  geom_col(data = precip_daily_join, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = dummy,
                group = 1,
                text = paste0("Date: ", date, "\n",
                              "Daily Cumulative Precipitation (in/10): ", precip_10_in)),
            size = 0.25,
            color = I("grey")) +
  theme_classic() + 
  labs(x = "Time", 
       y = " ", 
       title = "S2 Daily Cumulative Precipitation \n and Air Temperature Every 30min",
       subtitle = "Daily cumulative precipitation is plotted in grey. \n Air temperature is plotted in pink.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) 
    #+ scale_fill_identity(name = 'the fill', guide = 'legend',labels = c('m1')) +
    # scale_fill_manual(name = 'Color', 
    #      values = c("grey" = "grey", 
    #                 "pink" = "pink"), 
    #      labels = c("test", "test2"))

# + 
#   scale_y_continuous(
#     # Features of the first axis
#     name = "Air Temperature (ºC) / 100",
#     # Add a second axis and specify its features
#     sec.axis = sec_axis(trans = ~., name = "Cumulative Precipitation (in) / 10")
#   ) 

#static plot
#p_daily

# #save figure 
# ggsave(filename = "precip_daily_airtemp_30.png",
#        plot = p_daily,
#        path = "figures/",
#        width = 10,
#        height = 5,
#        units = c("in"),
#        dpi = 300)

#interactive plot
ggplotly(p_daily,
         tooltip = "text")

Streamflow + air temperature data (every 30min) + manual checks

For interactive plots,

  • air temperature (ºC / 100) is plotted as a pink line,

  • streamflow (ft) is plotted as a blue line,

  • and streamflow manual checkpoints (ft) are plotted as black dots.

#calculate the ratio of the 2 y-axes to plot the variables together in the static plot
trans_value <- max(airtemp_join$airtempc_100, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_air <- ggplot() + 
  #airtemp data
  geom_line(data = airtemp_join, 
            aes(x = datetime,
                y = airtempc_100, 
                group = 1,
                #create hovering labels for interactive graph 
                text = paste0("DateTime: ", datetime, "\n",
                              "AirTemp (ºC/100): ", airtempc_100)
                ),
            color = "pink", 
            alpha = 0.4) +
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft,
                 group = 1,
                 #create hovering labels for interactive graph 
                 text = paste0("DateTime: ", datetime, "\n",
                              "Stream Height (ft): ", round(stream_height_ft, digits = 3))
                 ),
            color = "blue",
            size = 0.75) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, 
                 y = stripchart_stage,
                 group = 1,
                 #create hovering labels for interactive graph 
                 text = paste0("DateTime: ", datetime, "\n",
                              "Manual Stream Height Check (ft): ", stripchart_stage)
                 ),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       y = " ",
       title = paste0("S2 Bog Streamflow and Air Temperature (", min_year, "-", max_year, ")"),
       subtitle = "Air temperature is plotted in pink. \n Streamflow is plotted in blue. \n Manual streamflow checkpoints are plotted as black dots.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) + 
  scale_y_continuous(
    # Features of the first axis
    name = "Air Temperature / 100 (ºC)",
    # Add a second axis and specify its features
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Stream Height (ft)")
  ) 

#static plot
#p_air

# #save the plot in the figures folder 
# ggsave(filename = "streamflow_airtemp_mc.png",
#        plot = p_air,
#        path = "figures/",
#        width = 8,
#        height = 4,
#        units = c("in"),
#        dpi = 300)

#interactive plot
ggplotly(p_air,
         tooltip = "text")

Streamflow + precip (daily) + air temp + manual checks

For interactive plots,

  • air temperature (ºC / 100) is plotted as a pink line,

  • cumulative daily precipitation (in / 10) is plotted as grey bars,

  • streamflow (ft) is plotted as a blue line,

  • and streamflow manual checkpoints (ft) are plotted as black dots.

#plot 
p_all <- ggplot() + 
  #airtemp data
  geom_line(data = airtemp_join, 
            aes(x = datetime,
                y = airtempc_100, 
                group = 1,
                #create hovering labels for interactive graph 
                text = paste0("DateTime: ", datetime, "\n",
                              "AirTemp (ºC/100): ", airtempc_100)
                ), 
            color = "pink",
            alpha = 0.4) +
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft,
                 group = 1,
                 #create hovering labels for interactive graph 
                 text = paste0("DateTime: ", datetime, "\n",
                              "Stream Height (ft): ", round(stream_height_ft, digits = 3))
                 ),
            color = "blue",
            size = 0.75) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, 
                 y = stripchart_stage,
                 group = 1,
                 #create hovering labels for interactive graph 
                 text = paste0("DateTime: ", datetime, "\n",
                              "Manual Stream Height Check (ft): ", stripchart_stage)
                 ),
             size = 0.5) + 
  #daily precipitation data 
  geom_col(data = precip_daily_join, 
            aes(x = datetime, 
                y = precip_10_in,
                group = 1,
                text = paste0("Date: ", date, "\n",
                              "Daily Cumulative Precipitation (in/10): ", precip_10_in)),
            colour = I("grey"),
            size = 0.25) +
  theme_classic() + 
  labs(x = "Time", 
       y = " ", 
       title = paste0("S2 Bog Streamflow, Air Temperature, \n and Daily Cumulative Precipitation (", min_year, "-", max_year, ")"),
       subtitle = "Air temperature is plotted in pink and scaled by 100. 
       Precipitation is plotted as grey bars and scaled by 10.
       Streamflow is plotted in blue.
       Manual streamflow checkpoints are plotted as black dots.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7),
        legend.position = "none") 
# + 
#   scale_y_continuous(
#     # Features of the first axis
#     name = "Air Temperature / 100 (ºC)",
#     # Add a second axis and specify its features
#     sec.axis = sec_axis(trans = ~.*trans_value, name = "Stream Height (ft)")
#   ) 

#static plot
#p_all

# #save the plot in the figures folder
# ggsave(filename = "airtemp_mc_precip_streamflow.png",
#        plot = p_all,
#        path = "figures/",
#        width = 8,
#        height = 4,
#        units = c("in"),
#        dpi = 300)

#interactive plot
ggplotly(p_all,
         tooltip = "text")

Note that this file is being knit as index.html into the air_comparisons repository in order to update the GitHub pages website, where the plots can be viewed online.